<?php
namespace app\admin\controller\goods;
class Cate extends \app\admin\controller\Common
{
    public function index(){
        return view();
    }

    public function menu(){
        $menu = (new \app\common\model\Classify())
        ->order('sort asc,id asc')
        ->select()->toArray();
        return self::successResult('',$menu);
    }
    public function list_to_tree($list, $pk='id', $pid = 'pid', $child = 'child', $root = 0) {
        // 创建Tree
        $tree = array();
        if(is_array($list)) {
            // 创建基于主键的数组引用
            $refer = array();
            foreach ($list as $key => $data) {
                $refer[$data[$pk]] =& $list[$key];
            }
            foreach ($list as $key => $data) {
                // 判断是否存在parent
                $parentId =  $data[$pid];
                if ($root == $parentId) {
                    $tree[] =& $list[$key];
                }else{
                    if (isset($refer[$parentId])) {
                        $parent =& $refer[$parentId];
                        $parent[$child][] =& $list[$key];
                    }
                }
            }
        }
        return $tree;
    }
    public function add()
    {
        if (request()->isPost()) {
            $data = request()->param();
            $validate = new \app\common\validate\Classify();
            $result = $validate->check($data);
            if ($result === false) {
                $this->error($validate->getError());
            }
            $table = new \app\common\model\Classify();
            $table->allowField(true)->save($data);
            if ($table->id) {
                return self::successResult();
            } else {
                return self::errorResult();
            }
        } else {
            $menu = (new \app\common\model\Classify())
                    ->order('sort desc,id desc')
                    ->select()->toArray();
            $menu = $this->list_to_tree($menu);
            $this->assign('menu',$menu);
            return view();
        }
    }
    
    public function edit()
    {
        if (request()->isPost()) {
            $data = request()->param();
            $validate = new \app\common\validate\Classify();
            $result = $validate->check($data);
            if ($result === false) {
                $this->error($validate->getError());
            }
            $table = new \app\common\model\Classify();
            $row = $table->allowField(true)->save($data, ['id' => $data['id']]);
        
            if ($row) {
                return self::successResult();
            } else {
                return self::errorResult();
            }
        } else {
            $id = intval(request()->param('id'));
            $info = (new \app\common\model\Classify())->where(['id' => $id])->find();
            $menu = (new \app\common\model\Classify())
                ->order('sort desc,id desc')
                ->select()->toArray();
            $menu = $this->list_to_tree($menu);
            $this->assign('menu',$menu);
            $this->assign('info',$info);
            return view();
        }
    }

    public function delete(){
        $id = request()->param('id');
        if (empty($id)) $this->error("请选择要删除的数据");
        $table = new \app\common\model\Classify();
        if (is_array($id)) {
            $id = implode(",", $id);
            $row = $table->whereIn('id', $id)->delete();
        } else {
            $row = $table->where('id', $id)->delete();
        }
        return $row ? self::successResult() : self::errorResult();
    }

  

 
}
